;Copyright 2007-2010 Niko Carpenter and Tyler Spivey
;    Linna Home is free software: you can redistribute it and/or modify
;    it under the terms of the GNU General Public License as published by
;    the Free Software Foundation, either version 3 of the License, or
;    any later version.

;    Linna Home is distributed in the hope that it will be useful,
;    but WITHOUT ANY WARRANTY; without even the implied warranty of
;    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;    GNU General Public License for more details.

;    You should have received a copy of the GNU General Public License
;    along with Linna Home.  If not, see http://www.gnu.org/licenses/.

#requireadmin
Opt("expandEnvStrings",1)
const $logallcmd = INIRead(@ScriptDir&"\settings.ini","settings","logallcmd","0")
Const $LIP = INIRead(@ScriptDir&"\settings.ini","bind","ip","0.0.0.0")
Const $LPort = INIRead(@ScriptDir&"\settings.ini","bind","port",1334)
Opt("OnExitFunc", "LinnaShutDownCaller")
INIWrite(@ScriptDir&"\settings.ini","bind","ip",$lip)
INIWrite(@ScriptDir&"\settings.ini","bind","port",$lport)
const $V_Type = "server"
Const $LogFile = @ScriptDir & "\Linna.log"
SRVLib_Init()
#NoTrayIcon
Break(0)
Global $Processes[1][3]
Global $IPCache[2]
$IPCache[0] = TimerInit()
$IPCache[1] = ""
$Processes[0][0] = -1
Enum $C_User, $C_Trust, $C_Parser, $C_Working, $C_REFilter, $C_REFilterFlag, $C_FTPPid, $C_FTPFile, $C_FTPDir, $C_pdata, $C_pdata2, $C_pdata3, $c_pdata4, $c_pdata5, $c_pdata6, $c_pdata7, $C_Prompt, $AR_Max
Global $Cons[1][$Ar_Max]
const $linver = "0.55"
If FileExists(@ScriptDir & "\banner.txt") Then
Const $WelcomeMSG = FileRead(@ScriptDir & "\banner.txt", FileGetSize(@ScriptDir & "\banner.txt"))
Else
Const $WelcomeMSG = "Linna Home"
EndIf
const $ftpFile = @ScriptDir&"\ftpfiles\ftpsd00000"
const $linvernotes = "Initial commit to Googlecode."
#include <screencapture.au3>
#include <inet.au3>
#Include <Connection.au3>
#Include <Parser.au3>
#Include <LCommands.au3>
#Include <Commands.au3>
#include <otr.au3>
#include <utils.au3>
opt("winTitleMatchMode",-2)
opt("WinDetectHiddenText",1)
global $blocked
Main()
Func Main()
While 1
doEvents()
SRVLib_DoServerEvents($Connection,"ConInit","Conclose","ParseCmd")
Sleep(1)
Wend
EndFunc

func doEvents()
for $i = 0 to uBound($Processes)-1
if $Processes[$i][0] >= 0 Then
$sr = STDOutRead($Processes[$i][1],1, 0)
if not @Error and $sr <> "" then
dSend($Processes[$i][0],STDOutRead($Processes[$i][1],0))
endIf
$sr = STDErrRead($Processes[$i][1],1, 0)
if not @error and $sr <> "" then
dSend($Processes[$i][0],STDErrRead($Processes[$i][1],0))
endIf
if not ProcessExists($Processes[$i][1]) Then
dSend($Processes[$i][0],"Finished: "&$Processes[$i][1]&" ("&$Processes[$i][2]&").")
if $Cons[$Processes[$i][0]][$C_Parser] = "cmdinteract" Then
dSend($Processes[$i][0],"Finished interacting with "&$Processes[$i][1])
lreturn($processes[$i][0])
EndIf
$Processes[$i][0] = -1
EndIf
EndIf
Next
if $blocked then
blockInput(1)
else
endif
;opt("ExpandEnvStrings",0)
for $i = 0 to uBound($Cons)-1
if $cons[$i][$C_User] = "" and $cons[$i][$C_FTPPid] > 0 Then
$olddir = @WorkingDir
FileChangeDir($Cons[$i][$C_FTPDir])
ProcessClose($cons[$i][$C_FTPPid])
FileDelete($cons[$i][$C_FTPFile])
FileChangeDir($OldDir)
EndIf
if $Cons[$i][$C_ftpPid] > 0 then
if not processExists($cons[$i][$C_ftpPid]) then
fileDelete($ftpFile&$i)
dSend($i,"ftp: "&$Cons[$i][$C_FTPFile]&" finished.")
$Cons[$i][$C_ftpPid] = 0
endIf
endIf
Next
endfunc

Func chkprm($id,$cmd)
local $Allowed = 0
if $Cons[$id][$C_Trust] = 1 Then
if iniread(@ScriptDir&"\groups.ini",INIRead(@ScriptDir&"\users.ini",$Cons[$id][$C_User],"group","trust1"),"lin_"&$cmd,"1") = 1 Then $Allowed = 1
ElseIf $Cons[$id][$C_Trust] = 0 Then
if iniread(@ScriptDir&"\groups.ini",INIRead(@ScriptDir&"\users.ini",$Cons[$id][$C_User],"group","trust0"),"lin_"&$cmd,"0") = 1 Then $Allowed = 1
EndIf
local $ini = iniread(@ScriptDir&"\users.ini",$Cons[$id][$C_User],"lin_"&$cmd,"999999")
if $INI = 1 Then
$Allowed = 1
ElseIf $ini = 0 Then
$Allowed = 0
EndIf
if $Allowed = 1 or $cmd = "exit" or $cmd = "help" or $cmd = "clear" or $cmd = "passwd" or $cmd = "echo" or $cmd = "prompt" or $cmd = "ver" Then
return 1
Else
return 0
EndIf
EndFunc

Func LinnaShutDown($ID, $reason = "")
If $id > -1 Then
$killedmsg = "Linna termination process initiated by "&$Cons[$id][$C_User]&" ("&$ID&")."
Else
$killedmsg = "Linna termination process initiated."
EndIf
If $Reason <> "" Then $KilledMsg &= @LF&"Reason: "&$Reason
SRVLib_SendAll($KilledMsg)
if $id > -1 Then dSend($id,"Killing user initiated processes")
for $i = 0 to uBound($Processes)-1
if $Processes[$i][0] >= 0 Then ProcessClose($Processes[$i][1])
Next
if $id > -1 Then dSend($id, "Ending ftp transfers.")
for $i = 0 to uBound($Cons)-1
if $cons[$i][$C_FTPPid] > 0 Then
dSend($i,@LF&"Your ftp transfer has been interupted.")
$olddir = @WorkingDir
FileChangeDir($Cons[$i][$C_FTPDir])
ProcessClose($cons[$i][$C_FTPPid])
FileDelete($cons[$i][$C_FTPFile])
FileChangeDir($olddir)
EndIf
Next
if $id > -1 and $Blocked Then dSend($ID, "Unblocking input.")
blockInput(0)
LogWrite($Killedmsg)
if $id > -1 Then dSend($ID, "Closing down server.")
SRVLib_ShutDown()
Exit
EndFunc
Func LinnaShutDownCaller() ;AutoIt had a problem with optional parameters; Function would raise an error stating that $id has not been declared when it was an optional parameter.
LinnaShutDown(-1)
EndFunc
Func AllowCheck($Sock)
local $Allow = 0
if iniread(@ScriptDir&"\Allow.ini","Allow","Trust","1") = 1 Then
if iniread(@ScriptDir&"\Allow.ini","Allow",SRVLib_SocketToIp($Sock),"1") = 1 Then $Allow = 1
ElseIf iniread(@ScriptDir&"\Allow.ini","Allow","Trust","0") = 0 Then
if iniread(@ScriptDir&"\Allow.ini","Allow",SRVLib_SocketToIp($Sock),"0") = 1 Then $Allow = 1
EndIf
Return $Allow
EndFunc

Func LogWrite($Msg)
$msg = @MON&"/"&@MDAY&"/"&@YEAR&" "&@HOUR&":"&@MIN&":"&@SEC&".  "&$MSG&@CRLF
if not $LogFile = "" Then
FileWrite($LogFile,$msg)
Else
ConsoleWrite($Msg)
EndIf
EndFunc

Func BackSpace($msg)
if $msg = "" Then Return ""
local $sp = StringSplit($msg,"")
local $sp2 = ""
for $i = 1 to $sp[0]
if $sp[$i] = CHR(8) Then
$sp2 = StringTrimRight($sp2,"1")
$sp[$i] = ""
EndIf
$sp2 &= $sp[$i]
Next
Return $sp2
EndFunc
